package xy.common.exception.global;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import xy.common.exception.dto.Result;
import xy.common.exception.dto.ResultEnum;

@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = MyException.class)
    public Result<String> bizExceptionHandler(MyException e) {
        log.error("发生业务异常！ msg: -> ", e);
        return Result.commonNoData(ResultEnum.BUSI_EXCEPTION);
    }

    /**
     * 处理空指针的异常
     */
    @ExceptionHandler(value = NullPointerException.class)
    public Result<String> exceptionHandler(NullPointerException e) {
        log.error("发生空指针异常！ msg: -> ", e);
        return Result.commonNoData(ResultEnum.NULL_POINTER_EXCEPTION);
    }

    /**
     * 服务器异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<String> exception(Exception e) {
        log.error("服务器异常！ msg: -> ", e);
        return Result.commonNoData(ResultEnum.INTERNAL_SERVER_ERROR_EXCEPTION);
    }
}
